package structures.review.sparse;

public class SparseArrayDemo {

    private static final int X = 10;
    private static final int Y = 10;

    public static void main(String[] args) {
        int[][] dataArrays = new int[X][Y];
        dataArrays[1][2] = 1;
        dataArrays[2][5] = 1;
        dataArrays[3][4] = 1;

        for (int x = 0; x < dataArrays.length; x++) {
            for (int y = 0; y < dataArrays[x].length; y++) {
                System.out.print(dataArrays[x][y] + "\t");
            }
            System.out.println();
        }

        int num = 0;
        for (int x = 0; x < dataArrays.length; x++) {
            for (int y = 0; y < dataArrays[x].length; y++) {
                if (dataArrays[x][y] != 0) {
                    num++;

                }
            }
        }
        if (num == 0) {
            System.out.println("没有需要保存的数据");
            return;
        }
        int[][] remainData = new int[num + 1][3];
        remainData[0][0] = dataArrays.length;
        remainData[0][1] = dataArrays[0].length;
        remainData[0][2] = num;
        int remainX = 1;
        for (int x = 0; x < dataArrays.length; x++) {
            for (int y = 0; y < dataArrays[x].length; y++) {
                if (dataArrays[x][y] != 0) {
                    remainData[remainX][0] = x;
                    remainData[remainX][1] = y;
                    remainData[remainX][2] = dataArrays[x][y];
                    remainX++;
                }
            }
        }
        for (int x = 0; x < remainData.length; x++) {
            for (int y = 0; y < remainData[x].length; y++) {
                System.out.print(remainData[x][y] + "\t");
            }
            System.out.println();
        }
    }
}
